<?php
class Admin_Home extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url','authentication_helper'));
		$this->load->library('session');
		$this->load->database();
	}
	
	function index(){
		doNeedLogin(uri_string());
		$this->load->model('pages_model');
		$rs = $this->pages_model->get_pages('home');
		$data['page'] = $rs->row_array();
		//$data['fields'] = $rs->list_fields();
		$data['fields'] = $this->db->list_fields('pages');
		$data['page_title'] = 'Home - Administration';
		$data['menu_item'] = 'Home';
		$data['user'] = $this->session->userdata('is_logged');
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/home', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	function edit(){
		doNeedLogin(uri_string());
		$this->load->model('pages_model');
		$id = $this->input->post("ID");
		$description = $this->input->post("description");
		$youtube_url = $this->input->post("youtube_id");
		$youtube_id = getYoutubeID($youtube_url);
		$data = array(
               'description' => $description,
			   'youtube_link' => $youtube_id,
        ); 
		$this->db->update('pages', $data, array('page_name' => $id));
		if ($this->db->affected_rows() > 0) {
			echo json_encode(array('success'=>true,"id"=>$youtube_id));
		} else {
			echo json_encode(array('success'=>false,"reason"=>"query update not sucessful"));
		} 
	}
}
